package com.kk.opengles.screenshotdemo;

import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.kk.opengles.screenshotdemo.utils.ScreenUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    ExecutorService mExecutorService = Executors.newSingleThreadExecutor();

    VideoView mVideoView;
    ImageView mImageView1;
    ImageView mImageView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        mVideoView = findViewById(R.id.vvVideo);
        mImageView1 = (ImageView) findViewById(R.id.img_1);
        mImageView2 = (ImageView) findViewById(R.id.img_2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.btScreenshot01).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mExecutorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = ScreenUtils.getScreenshot(MainActivity.this);
                        Log.i(TAG, "run001: " + bitmap);
                        if(bitmap!=null)
                        {
                            if(bitmap!=null)
                            {
                                ScreenUtils.saveBitmap(bitmap,"/sdcard/001.png");
                            }
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.i(TAG, "run: ");
                                    mImageView1.setImageBitmap(bitmap);
                                }
                            });
                        }
                    }
                });
            }
        });

        findViewById(R.id.btScreenshot02).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mExecutorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = ScreenUtils.getScreenshot2(MainActivity.this);
                        Log.i(TAG, "run002: " + bitmap);
                        if(bitmap!=null)
                        {
                            ScreenUtils.saveBitmap(bitmap,"/sdcard/002.png");
                        }
                        if(bitmap!=null)
                        {
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.i(TAG, "run: ");

                                    mImageView2.setImageBitmap(bitmap);
                                }
                            });

                        }
                    }
                });
            }
        });

        initVideo();
    }

    private void initVideo() {
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.i(TAG, "onError: what:"+what +" extra:"+extra);
                return true;
            }
        });

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.start();
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

            }
        });

        mVideoView.setVideoPath("http://shinemov.joyk.com.cn/free/844065.ts");

    }
}